home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Telnet 2.7b5 / source / tek / tekstor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-01  |  5.1 KB  |  244 lines  |  [TEXT/CWIE]

  1. /* This has been rewritten to use one damn handle to hold all of the data. -- JMB */
  2.  
  3. #ifdef MPW
  4. #pragma segment TEKMAIN
  5. #endif
  6.  
  7. #include "vgtek.proto.h"
  8. #include "rg0.proto.h"
  9. #include "rgmp.proto.h"
  10. #include "tekrgmac.proto.h"
  11. #include "tekdefs.h"    /* NCSA: sb - all defines are now here, for easy access */
  12. #include "tekstor.proto.h"
  13.  
  14. //    thiselnum runs from 0 to (handlesize-1), which is in accordance with C style
  15. //    arrays.  The handle is indexed 0...(handlesize-1) as well.  This is why you see
  16. //    the various +-1's. -- JMB 8/93
  17.  
  18. TEKSTOREP    newTEKstore(void)
  19. {
  20.     TEKSTOREP    s;
  21.     
  22.     s = (TEKSTOREP) myNewPtr(sizeof(TEKSTORE));
  23.     if (s == NULL) return (NULL);
  24.     
  25.     if (!(s->dataHandle = myNewHandle(0L)))
  26.         return (NULL);
  27.     
  28.     s->thiselnum = 0;
  29.     
  30.     return(s);
  31. }
  32.  
  33. void freeTEKstore(TEKSTOREP s)
  34. {
  35.     DisposeHandle(s->dataHandle);
  36.     DisposePtr((Ptr) s);
  37. }
  38.  
  39. short    addTEKstore(TEKSTOREP s, char d)
  40. {
  41.     Size    handlesize;
  42.     OSErr    memErr;
  43.     handlesize = GetHandleSize(s->dataHandle);
  44.     memErr = mySetHandleSize(s->dataHandle, ++handlesize);
  45.     if (memErr) return (-1);
  46.     
  47.     (*(s->dataHandle))[handlesize-1] = d;    // Save in last position
  48.     
  49.     return(0);
  50. }
  51.  
  52. void    topTEKstore(TEKSTOREP s)
  53. {
  54.     s->thiselnum = 0;
  55. }
  56.  
  57. short    nextTEKitem(TEKSTOREP s)
  58. {
  59.     if (s->thiselnum == (GetHandleSize(s->dataHandle))) return(-1);    // At end of data
  60.     
  61.     return((short) ((*(s->dataHandle))[(s->thiselnum)++]) );
  62. }
  63.  
  64. short    TEKunstore(TEKSTOREP s)
  65. {
  66.     Size    handlesize;
  67.     OSErr     memErr;
  68.     if ((handlesize = GetHandleSize(s->dataHandle)) == 0) return (-1);    // Nothing in store
  69.         
  70.     memErr = mySetHandleSize(s->dataHandle, --handlesize);
  71.     if (memErr) return(-1);
  72.     
  73.     if (s->thiselnum >= handlesize) s->thiselnum = handlesize - 1;
  74.     return(0);
  75. }
  76.  
  77. #ifdef    AARON_CONTORER_HAS_A_BRAIN        // Never true
  78. #define MINPOOL 0x0200    /* smallest allowable pool     */
  79. #define MAXPOOL 0x2000    /* largest allowable pool     */
  80.  
  81. STOREP newstore(void)
  82. /* 
  83.     create a new, empty store and return a pointer to it.
  84.     Returns NULL if not enough memory to create a new store.
  85. */
  86. {
  87.     STOREP s;
  88.     
  89.     s=(STOREP) myNewPtr((long) sizeof(STORE));            /* BYU LSC */
  90.     if (s==NULL) {
  91.         return(NULL);
  92.     }
  93.     else {
  94.         s->lasth = s->thish = s->firsth =
  95.             (HANDLEP) myNewPtr((long) sizeof(HANDLE));    /* BYU LSC */
  96.         if (s->firsth==NULL) {
  97.             DisposPtr((Ptr) s);
  98.             return(NULL);
  99.         }
  100.         else {
  101.             s->firsth->pool = myNewPtr((long) MINPOOL);    /* BYU LSC */
  102.             if (s->firsth->pool==NULL) {
  103.                 DisposPtr((Ptr) s->firsth);
  104.                 DisposPtr((Ptr) s);
  105.                 return(NULL);
  106.             }
  107.             else {
  108.                 s->lastelnum = s->thiselnum = -1;
  109.                 s->firsth->poolsize = MINPOOL;
  110.                 s->firsth->next = NULL;
  111.             }
  112.         }
  113.     }
  114.     return(s);
  115. }
  116.  
  117. void freestore(STOREP s)
  118. /*
  119.     Frees all pools and other memory space associated with store s.
  120. */
  121. {
  122.     HANDLEP h,h2;
  123.     h = s->firsth;
  124.     while (h != NULL) {
  125.         h2 = h;
  126.         DisposPtr(h->pool);
  127.         h = h->next;
  128.         DisposPtr((Ptr) h2);
  129.     }
  130.     DisposPtr((Ptr) s);
  131. }
  132.  
  133.  
  134. int addstore(STOREP s, char d)
  135. /*
  136.     Adds character d to the end of store s.
  137.     Returns 0 if successful, -1 if unable to add character (no memory).
  138. */
  139. {
  140.     int n; /* temp storage */
  141.     long size;                    /* BYU LSC */
  142.     HANDLEP h;
  143.  
  144.     n = ++(s->lastelnum);
  145.     size = s->lasth->poolsize;
  146.     if (n < s->lasth->poolsize) {
  147.         s->lasth->pool[n] = d;
  148.     }
  149.     else {
  150.         /* Pool full; allocate a new one. */
  151.         if (size<MAXPOOL) size <<= 1;
  152.         h = (HANDLEP)myNewPtr((long) sizeof(HANDLE));        /* BYU LSC */
  153.         if (h==NULL) {
  154.             (s->lastelnum)--;
  155.             return(-1);
  156.         }
  157.         else {
  158.             h->pool = myNewPtr(size);
  159.             if (h->pool==NULL) {
  160.                 DisposPtr((Ptr) h);
  161.                 (s->lastelnum)--;
  162.                 return(-1);
  163.             }
  164.             else {
  165.                 h->poolsize = size;
  166.                 h->next = NULL;
  167.                 s->lasth->next = h;
  168.                 s->lasth = h;
  169.                 s->lastelnum = 0;
  170.                 h->pool[0] = d;
  171.             }
  172.         }
  173.         } /* end of new pool allocation */
  174.     return(0);
  175. } /* end addstore() */
  176.  
  177. topstore(STOREP s)
  178. /*
  179.     Reset stats so that a call to nextitem(s) will be retrieving the
  180.     first item in store s.
  181. */
  182. {
  183.     s->thish = s->firsth;
  184.     s->thiselnum = -1;
  185. }
  186.  
  187. int nextitem(STOREP s)
  188. /*
  189.     Increment the current location in store s.  Then return the
  190.     character at that location.  Returns -1 if no more characters.
  191. */
  192. {
  193.     HANDLEP h;
  194.  
  195.     if (s->thish==s->lasth && s->thiselnum==s->lastelnum) return(-1);
  196.     else {
  197.         h = s->thish;
  198.         if (++(s->thiselnum) < s->thish->poolsize) {
  199.             return((int)(s->thish->pool[s->thiselnum]));
  200.             }
  201.         else {
  202.             /* move to next pool */
  203.             h = h->next;
  204.             s->thish = h;
  205.             s->thiselnum = 0;
  206.             return((int)(h->pool[0]));
  207.             }
  208.         }
  209. } /* end nextitem() */
  210.  
  211. int unstore(STOREP s)
  212. /*
  213.     Removes ("pops") the last item from the specified store.
  214.     Returns that item (in range 0-255), or returns -1 if there
  215.     are no items in the store.
  216. */
  217. {
  218.     HANDLEP nextolast;
  219.  
  220.     if (s->lastelnum > -1) { /* last pool not empty */
  221.         return((int)(s->lasth->pool[(s->lastelnum)--]));
  222.     } else { /* last pool empty */
  223.         if (s->lasth == s->firsth) return(-1);
  224.  
  225.         else { /* move back one pool */
  226.             nextolast = s->firsth;
  227.             while (nextolast->next != s->lasth)
  228.                 nextolast = nextolast->next;
  229.             DisposPtr((Ptr) nextolast->next);
  230.             s->lasth = nextolast;
  231.             s->lastelnum = nextolast->poolsize - 2;
  232.  
  233.             if (s->thish == nextolast->next) {
  234.                 s->thish = nextolast;
  235.                 s->thiselnum = s->lastelnum;
  236.             }
  237.  
  238.             nextolast->next = NULL;
  239.             return((int)(nextolast->pool[s->lastelnum+1]));
  240.         }
  241.     }
  242. }
  243. #endif
  244.